<?php
if(!defined('PATH')) exit();

$status = ($player->isOnline()) ? '<span style="color:green;font-size:10px;">ONLINE</span>' : '<span style="color:red;font-size:10px;">OFFLINE</span>';
$deletion = ($player->isDeleted()) ? '<span style="color:red;font-size:10px;"> - DELETED</span>' : '';
$namelock = ($player->isNameLocked()) ? '<span style="color:red;font-size:10px;"> - NAMELOCKED</span>' : '';
$lastlogin = (!$player->getLastLogin()) ? 'never logged in' : date($this->config['website']['date_format'], $player->getLastLogin());
$account = $player->getAccount();
?>
<table border="0" cellpadding="8" cellspacing="1" width="100%">
<tr class="head"><td colspan="2"><b>Profile</b></td></tr>
<tr><td width="20%">Name:</td><td><?=$player->getName()?></td></tr>
<tr><td>Status:</td><td><?=$status . $deletion . $namelock?> </td></tr>
<tr><td>Sex:</td><td><?=$this->archez->getSexName($player->getSex())?></td></tr>
<tr><td>Profession:</td><td><?=$this->config['server']['professions'][$player->getPromotion()][$player->getVocation()]?></td></tr>
<tr><td>Level:</td><td><?=$player->getLevel()?></td></tr>
<tr><td>Residence:</td><td><?=$this->config['server']['towns'][$player->getTownId()]?></td></tr>
<?php
$rank_of_player = $player->getRank();
if(!empty($rank_of_player))
{
	$guild_name = $rank_of_player->getGuild()->getName();
	$guild_id = $rank_of_player->getGuild()->getId();
?>
<tr><td>Guild membership:</td><td><?=$rank_of_player->getName()?> of the <a href="<?=PATH?>/guilds/show/<?=$guild_id?>/"><?=$guild_name?></a></td></tr>
<?php
}
?>
<?php
$house = $this->db->query('SELECT `id`, `name` FROM `houses` WHERE `owner` = '.$player->getId())->fetch();
if ($house) {
?>
<tr><td>House:</td><td><a href="<?=PATH?>/houses/show/<?=$house['id']?>/"><?=$house['name']?></a></td></tr>
<?php
}
?>
<tr><td>Last login:</td><td><?=$lastlogin?></td></tr>
<tr><td>Created:</td><td><?=date($this->config['website']['date_format'],$player->getCustomField('created'))?></td></tr>
<?php
if($player->getCustomField('comment'))
{
?>
	<tr><td>Comment:</td><td><?=nl2br($player->getCustomField('comment'))?></td></tr>
<?php
}
?>
</table>
<?php
$this->archez->loadInjections('search/check', get_defined_vars());

// I know it's very bad way, because it's in view...
$dead_add_content = null;
$player_deaths = $this->db->query('SELECT `id`, `date`, `level` FROM `player_deaths` WHERE `player_id` = '.$player->getId().' ORDER BY `date` DESC LIMIT 0,10;');
if(count($player_deaths))
{
	$n = 1;
	foreach($player_deaths as $death)
	{

		$dead_add_content .= "<tr><td>".$n."</td><td><small>".date($this->config['website']['date_format'], $death['date'])."</small></td><td>";
		$killers = $this->db->query("SELECT environment_killers.name AS monster_name, players.name AS player_name, players.deleted AS player_exists FROM killers LEFT JOIN environment_killers ON killers.id = environment_killers.kill_id LEFT JOIN player_killers ON killers.id = player_killers.kill_id LEFT JOIN players ON players.id = player_killers.player_id WHERE killers.death_id = '".$death['id']."' ORDER BY killers.final_hit DESC, killers.id ASC")->fetchAll();

		$i = 0;
		$count = count($killers);
		foreach ($killers as $killer) {
			$i++;
			if ($killer['player_name'] != "") {
				if ($i == 1)
					$dead_add_content .= "Killed at level <b>" . $death['level'] . "</b>";
				else if ($i == $count)
					$dead_add_content .= " and";
				else
					$dead_add_content .= ",";

				$dead_add_content .= " by ";
				if ($killer['monster_name'] != "")
					$dead_add_content .= $killer['monster_name'] . " summoned by ";

				if ($killer['player_exists'] == 0)
					$dead_add_content .= "<a href=\"" . PATH . "/search/check/" . urlencode($killer['player_name']) . "/\">";

				$dead_add_content .= $killer['player_name'];
				if ($killer['player_exists'] == 0)
					$dead_add_content .= "</a>";
			}
			else {
				if ($i == 1)
					$dead_add_content .= "Died at level <b>" . $death['level'] . "</b>";
				else if ($i == $count)
					$dead_add_content .= " and";
				else
					$dead_add_content .= ",";

				$dead_add_content .= " by " . $killer['monster_name'];
			}
		}

		$dead_add_content .= ".</td></tr>";
		$n++;
	}
}
if ($dead_add_content) {
?>
<br /><table border="0" cellpadding="8" cellspacing="1" width="100%">
<tr class="head"><td colspan="3"><b>Deaths</b></td></tr>
<tr class="bold"><td width="5%">#</td><td width="30%">Date</td><td width="65%">Information</td></tr>
<?=$dead_add_content?>
</table>
<?php
}
if ($player->getHideChar() == 0) {
?>
<br/>
<table border="0" cellpadding="8" cellspacing="1" width="100%">
<tr class="head"><td colspan="2"><b>Account Information</b></td></tr>
<?php
	$acc_status = ($account->isPremium()) ? '<b><font color="green">Premium Account</font></b>' : '<b><font color="red">Free Account</font></b>';
	if ($rlname = $account->getCustomField('rlname')) {
?>
<tr><td width="20%">Real name:</td><td><?=$rlname?></td></tr>
<?php
	}
	if ($location = $account->getCustomField('location')) {
?>
<tr><td width="20%">Location:</td><td><?=$location?></td></tr>
<?php
	}
?>
<tr><td width="20%">Created:</td><td><?=date($this->config['website']['date_format'],$account->getCustomField('created'))?></td></tr>
<tr><td width="20%">Account status:</td><td><?=$acc_status?></td></tr>
</table>

<?php
	$characters = $account->getPlayersList();
	$count = $this->db->query('SELECT COUNT(*) as `n` FROM `players` WHERE `account_id` = '.$account->getId().' AND `hide_char` = 0 AND `deleted` = 0;')->fetch();
	$count = $count['n'];
	if ($count > 1) {
?>
<br /><table border="0" cellpadding="8" cellspacing="1" width="100%">
<tr class="head"><td colspan="4"><b>Characters</b></td></tr>
<tr class="bold"><td width="50%">Name</td><td width="15%">Level</td><td width="25%">Vocation</td><td>Status</td></tr>
<?php
		$characters->orderBy('name');
		
		foreach($characters as $row)
		{
			if ($row->getCustomField('hide_char') != 0 || $row->getId() == $player->getId())
				continue;
			if ($row->isOnline())
			{
				$row_status = '<span style="color:green;">online</span>';
			}
			else
			{
				$row_status = '<span style="color:red;">offline</span>';
			}
			
			if (!$row->isDeleted()) {
				if (!$row->getHideChar()) {
?>
	<tr><td><a href="<?=PATH?>/search/check/<?=urlencode(strtolower($row->getName()))?>/"><?=$row->getName()?></a></td><td><?=$row->getLevel()?></td><td><?=$this->config['server']['professions'][$row->getPromotion()][$row->getVocation()]?></td><td><?=$row_status?></td></tr>
<?php
				}
			}
		}
	}
}
?>
</table>

<form action="<?=PATH?>/search/check/" method="post">
<br /><table border="0" cellpadding="8" cellspacing="1" width="100%">
<tr class="head"><td colspan="2"><b>Character search</b></td></tr>
<tr><td width="20%">Name:</td><td><input type="text" name="name" value="<?=$player->getName()?>" /></td></tr>
<tr><td colspan="2"><input type="submit" value="Submit" /></td></tr>
</table>
</form>